﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ORM.Business.Feedbacks;
using ORM.Business.Interfaces;
using ORM.Business.Models.Entities;
using ORM.Common.Logging;
using ORM.Resources;

namespace ORM.Business.Repositories
{
    public class DeviceRepository : IDeviceRepository
    {
        OrmEntities ORMDbContext = new OrmEntities();       
        public Feedback<IList<Device>> GetAllDevice()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    var devices =
                        entities.Devices.Where(t => !t.IsDeleted).OrderBy(t => t.Name).ToList();
                    return new Feedback<IList<Device>>(true, null, devices);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<IList<Device>>(false, Messages.GeneralError);
            }
        }


        public Feedback<Device> InsertDevice(Device t)
        {
            Device tnew = new Device
            {
                Name = t.Name,
                //NameUrl = t.NameUrl,
                Description = t.Description,
                IsDeleted = false
            };

            try
            {
                using (var entities = new OrmEntities())
                {
                    //Check for duplicate name
                    var exist = entities.Devices
                        .FirstOrDefault(i => i.Name == tnew.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<Device>(false, Messages.InsertCategory_DuplicateName);

                    entities.Devices.Add(tnew);
                    entities.SaveChanges();
                }
                return new Feedback<Device>(true, Messages.InsertDeviceCategorySuccess, tnew);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Device>(false, Messages.GeneralError);
            }
        }

        public Feedback<IList<Device>> GetAllDeviceCategories()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    var categories =
                        entities.Devices.Where(t => !t.IsDeleted).OrderBy(t => t.Name).ToList();
                    return new Feedback<IList<Device>>(true, null, categories);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<IList<Device>>(false, Messages.GeneralError);
            }
        }

        public Feedback<Device> GetDeviceCategoryDetails(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    Device category = entities.Devices.SingleOrDefault(i => i.Id == id && !i.IsDeleted);
                    if (category != null)
                        return new Feedback<Device>(true, null, category);
                    else
                        return new Feedback<Device>(false, Messages.GetCategory_NotFound);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Device>(false, Messages.GeneralError);
            }
        }

        public Feedback<Device> UpdateDeviceCategory(Device c)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    // Check for duplicate name
                    Device exist = entities.Devices.FirstOrDefault(i => i.Id != c.Id && i.Name == c.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<Device>(false, Messages.InsertCategory_DuplicateName);

                    var category = entities.Devices.Single(i => i.Id == c.Id);
                    category.Name = c.Name;
                    category.Description = c.Description;

                    entities.SaveChanges();

                    return new Feedback<Device>(true, null, category);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Device>(false, Messages.GeneralError);
            }
        }

        public Feedback DeleteDeviceCategory(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    foreach (var model in entities.Models.Where(i => i.DeviceId == id).ToList())
                    {
                        model.IsDeleted = true;
                        foreach (var guide in entities.Guides.Where(i => i.ModelID == model.Id).ToList())
                        {
                            guide.IsDeleted = true;
                        }
                        foreach (var question in entities.Questions.Where(i => i.ModelId == model.Id).ToList())
                        {
                            question.IsDeleted = true;
                        }
                    }
                    Device c = entities.Devices.Single(i => i.Id == id);
                    c.IsDeleted = true;
                    entities.SaveChanges();

                    return new Feedback(true);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback(false, Messages.GeneralError);
            }
        }        
    }
}